package com.thd.springboottest.redis.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

@Component
public class RedisConsumer implements ApplicationRunner {
    @Autowired
    //redis连接池
    private JedisPool jedisPool;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("------------- 启动后在这里做一些其他事情,redis消费者监听 -------------");

        Jedis jedis = jedisPool.getResource();
        jedis.subscribe(new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println("收到消息：" + message);
            }
        }, "mq_message");

    }
}
